using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace MTRC.ODMS.Web.SOM.Popup
{
    /// <summary>
    /// Author: Huangyao
    /// </summary>
    public partial class SignatureFileSave : BasePage_SOM
    {
        #region MenuID

        //protected override string MenuID
        //{
        //    get
        //    {
        //        return "M141501";
        //    }
        //}

        #endregion

        #region Page Init And Load

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            this.ChangeLanguage += new ChangeLanguageEventHandler(ChangeLanguage_Function);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.SetLanguage();
                this.SaveSignatureFile();
            }
        }

        #endregion

        private void SaveSignatureFile()
        {
            string bmpSource = Request.Form["rgb_array"];

            int photow = 100;
            int photoh = 100;

            string[] rgbArray = bmpSource.Split(new char[] { ',' });
            //string path = HttpContext.Current.Server.MapPath("~/images/1.bmp");
            string fullFileName = APJ.Util.Utility.GetAppSetting("SOM_SignatureFileTempPath") + "Signature_" + Guid.NewGuid().ToString() + ".bmp";
            this.DrawImageFromFlashUploader(photow, photoh, rgbArray).Save(fullFileName);

            Response.Redirect("/Test/HY_WebForm.aspx?FullFileName=" + fullFileName);
        }

        private System.Drawing.Bitmap DrawImageFromFlashUploader(int width, int height, string[] rgbArray)
        {
            System.Drawing.Bitmap bm = new System.Drawing.Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
            int pos = 0;
            for (int i = bm.Height - 1; i >= 0; i--)
            {
                for (int j = 0; j < bm.Width; j++)
                {
                    bm.SetPixel(j, i, System.Drawing.Color.FromArgb(int.Parse(rgbArray[pos], System.Globalization.NumberStyles.HexNumber)));
                    pos++;
                }
            }

            return bm;
        }  

        #region SetLanguage

        void ChangeLanguage_Function(object sender, EventArgs e)
        {
            SetLanguage();
        }

        private void SetLanguage()
        {
            //
        }

        #endregion
    }
}
